package com.wenqiner.requesttool.util.file;

import cn.hutool.core.io.FileUtil;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @author rhf
 * @date 2023-03-24
 **/
public abstract class AbstractFileParser {

    public static final String FIELD_SEPARATOR = ",";

    /**
     * 获取文件流
     *
     * @param filePath 文件路径
     * @return java.io.BufferedReader
     * @author rhf
     */
    protected BufferedReader inputStream(String filePath) throws FileNotFoundException {
        return new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
    }

    /**
     * 获取PrintWriter
     *
     * @param filePath 文件路径
     * @return java.io.PrintWriter
     * @author rhf
     */
    protected PrintWriter getWriter(String filePath) throws IOException {
        File file = new File(filePath);
        FileUtil.del(file);
        boolean newFile = file.createNewFile();
        return new PrintWriter(new OutputStreamWriter(new FileOutputStream(file)), true);
    }

    /**
     * 解析文件
     *
     * @param filePath 文件路径
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.String>>
     * @author rhf
     */
    public abstract List<Map<String, String>> parse(String filePath);

    /**
     * 写入到文件
     *
     * @param filePath  文件路径
     * @param data      数据
     * @param fieldName 字段名
     * @author rhf
     */
    public abstract void writerToFile(String filePath, List<Map<String, String>> data, List<String> fieldName);
}
